#!/usr/bin/env python

'''
    This is a utility script which does all the bits & pieces needed to create
    the directory for the web server.
'''

import os, sys, shutil, time

DEST = 'sorting'

def usage():
    print 'Usage:', sys.argv[0], 'sis.tgz'
    print '''
    Create a "%s" directory suitable for viewing on the web.

    Everything is unpacked, setup and put into the www directory.
    The sis.tgz argument specifies the path to the tarball from the SIS
    (containing reviewer and submission information.''' % (DEST)
    sys.exit(0)

if len(sys.argv) != 2:
    usage()

def progress(msg):
    n = (76 - len(msg))//2
    print '='*n, msg, '='*n

def error(msg):
    print '\n\nERROR: %s' % msg
    sys.exit(1)

def main():
    if os.path.exists(DEST):
        print '%s directory already exists, bye...' % DEST
        sys.exit(1)

    os.mkdir(DEST)
    files = [
        sys.argv[1],    # Copy over SIS tarball
        'sis/map2sis.py',
        'sis/sis2txt.py',
        'sis/sismenus.py',
        'sis/txt2map.py',
        'sis/txtnames.py',
        'sis/requests-2011.py',
        'www/edit.php',
        'www/index.php',
        'www/header.php',
        'www/util.py',
        'www/display.py',
        'autosort.pl',
        'www/js',
        'www/css',
    ]

    progress('Copying files...')
    for f in files:
        print '  ', f
        if os.path.isdir(f):
            dest = os.path.join(DEST, os.path.basename(f))
            shutil.copytree(f, dest)
        else:
            shutil.copy(f, DEST)

    # Move into the destination directory and do operations locally
    os.chdir(DEST)

    progress('Extracting files')
    if os.path.splitext(sys.argv[1])[1] == '.zip':
        if os.system('unzip -v %s' % sys.argv[1]) != 0:
            error('extracting tar files')
    else:
        if os.system('tar xf %s' % sys.argv[1]) != 0:
            error('extracting tar files')

    progress('Initializing web directories')
    if os.system('python display.py init') != 0:
        error('Initializing directories')

    progress('Extracting and preparing SIS data')
    if os.system('python sis2txt.py') != 0:
        error('Error stripping the SIS HTML files')
    if os.system('python txt2map.py') != 0:
        error('Error in mapping to sequential indices')

    # For 2011, run custom requests
    if time.localtime().tm_year == 2011:
        if os.system('python requests-2011.py') != 0:
            error('Error generating system requests')

    # Now, generate the javascript menu code
    if os.system('python sismenus.py') != 0:
        error('Error creating javascript menus')

    # Update permissions on sorting/run/pref
    progress('Updating file system permissions')
    os.chmod('run',0777)
    os.chmod('run/prefs',0777)

    print '''
        Success!

        - Make the %s directory visible to the web server
        - Point your browser to the right place
        - Don't forget to add SRC expertise to the SRC jurors

        If you can, please run txtnames.py in the sorting directory.

        When you're done, go to the %s directory and run:
            "python map2sis.py"
    ''' % (DEST, DEST)

if __name__ == "__main__":
    main()
